<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <!-- <img class="avatar" src="@/assets/images/avatar.png" alt="" srcset="" /> -->
      </div>

      <!-- 登录区域 -->
      <el-form
        class="form"
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="loginForm"
      >
        <!-- 输入区域 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            ref="password"
            prefix-icon="el-icon-lock"
            type="password"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button class="submit" type="primary" @click="login"
            >登录</el-button
          >
          <el-button class="reset" type="info" @click="resetLoginForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setToken } from '../../utils/auth'
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm() {
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.loginFormRef.resetFields()
    },

    // 登录
    login() {
      // 验证所有表单项
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          this.$message.error('输入有误, 请重新输入')
          return
        }

        const { data: res } = await this.$post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功!')
        setToken(res.data.token)
        this.$router.push({ name: 'User' })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  padding-top: 160px;

  .login-box {
    width: 520px;
    min-height: 360px;
    position: relative;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 8px;

    .avatar-box {
      width: 100px;
      height: 100px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      background-image: linear-gradient(
        to bottom right,
        skyblue,
        rgb(255, 115, 0)
      );
      box-shadow: 0 0 10px skyblue;
      border-radius: 50%;

      .avatar {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .form {
      position: absolute;
      left: 0;
      bottom: 0;
      padding: 20px;
      box-sizing: border-box;
      width: 100%;

      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
